package com.wyd.studentboot.controller;

import com.wyd.studentboot.bean.Msg;
import com.wyd.studentboot.bean.SignIn;
import com.wyd.studentboot.bean.UserBean;
import com.wyd.studentboot.service.SignInService;
import com.wyd.studentboot.service.StudentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class SignInController {
    @Resource
    private SignInService service;
    /**
     * 日签到
     * @param request
     * @return
     */
    @RequestMapping(value = "/sign")
    public Msg add(HttpServletRequest request){
        //获取用户数据
        UserBean  user =(UserBean) request.getSession().getAttribute("user");
        if(user==null)return new Msg(0,"抱歉，检测到您未登录！");
        System.out.println(user.getStudent_id());
        int code = service.add(new SignIn(user.getStudent_id(),1));
        if(code==1){
            return new Msg(1,"恭喜，签到成功！");
        }else if(code==2){
            return new Msg(code,"您今日已签到！无需再操作。");
        }else{
            return new Msg(0,"抱歉，签到失败！");
        }
    }

    /**
     * 签到历史数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/sign/history")
    public List<SignIn> history(HttpServletRequest request){
        //获取用户信息
        UserBean user = (UserBean) request.getSession().getAttribute("user");
        if(user==null)return null;
        List<SignIn> signs = service.history(user.getStudent_id());
        return signs;
    }
}
